﻿using System;
using System.Data;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
using Dcms.BLL;
using Dcms.Model;
using Dcms.Common;
using AjaxPro;
using Dcms.UpdateModule;
using WebApp.AppCode;

public partial class Manage_Modle_Link_EditLink1 : System.Web.UI.Page
{
    CompetenceLib CL = new CompetenceLib();

    Dcms_CateBLL cBll = new Dcms_CateBLL();
    Dcms_LinkBLL lBll = new Dcms_LinkBLL();

    /// <summary>
    /// 页面加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        CL.IsLogin();
        try
        {
            if (!IsPostBack)
            {
                if (strCatID != "")
                {
                    Dcms_CateModel cModel = cBll.GetModelByID(Convert.ToInt32(strCatID));
                    if (cModel != null)
                    {
                        txb_CateName.Text = cModel.Cate_Name;
                        txb_CateName.Enabled = false;
                        btnCopy.Enabled = false;
                        //btnSave.Enabled = false;
                        btnDel.Enabled = false;

                        Dcms_LinkModel lModel = null;
                        //判断展示方式
                        if (cModel.Cate_showtype == 0)
                        {
                            //处理单条
                            lModel = lBll.GetNewModelByCatID(cModel.Cate_ID);
                            if (lModel != null)
                            {
                                //初使化数据
                                DcmsOperateModel objDcmsOperateModel = new DcmsOperateModel();
                                objDcmsOperateModel.InitControl(lModel, this.Form.Controls);
                            }
                        }
                        else
                        {
                            if (strAction == "edit")
                            {
                                //处理多条
                                lModel = lBll.GetModelByID(Convert.ToInt32(strNewsID));
                                if (lModel != null)
                                {
                                    btnDel.Visible = true;
                                    btnCopy.Visible = true;
                                    btnSave.Enabled = true;
                                    //初使化数据
                                    DcmsOperateModel objDcmsOperateModel = new DcmsOperateModel();
                                    objDcmsOperateModel.InitControl(lModel, this.Form.Controls);
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_Modle_Link_EditLink.Page_Load", ex);
            throw ex;
        }
    }

    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            Dcms_CateModel cModel = cBll.GetModelByID(Convert.ToInt32(strCatID));
            if (cModel != null)
            {
                Dcms_LinkModel lModel = null;
                if (cModel.Cate_showtype == 0)
                {
                    lModel = lBll.GetNewModelByCatID(cModel.Cate_ID);
                    if (lModel != null)
                    {
                        //已存在
                        DcmsOperateModel objDcmsOperateModel = new DcmsOperateModel();
                        objDcmsOperateModel.UpdateModelByControls(lModel, this.Form.Controls);
                        lBll.Update(lModel);
                    }
                    else
                    {
                        //不存在
                        lModel = new Dcms_LinkModel();
                        DcmsOperateModel objDcmsOperateModel = new DcmsOperateModel();
                        objDcmsOperateModel.UpdateModelByControls(lModel, this.Form.Controls);
                        lModel.Link_CateId = cModel.Cate_ID;
                        lBll.AddModel(lModel);
                    }
                    Session.Add("Message", "信息已经编辑成功，请确认！");
                    Session.Add("OtherMessage", "");
                    Session.Add("Url", "Modle/Link/EditLink" + cModel.Cate_ModelKeyId + ".aspx?CatID=" + strCatID);
                    Response.Redirect("../../Message.aspx", false);
                }
                else
                {
                    if (strAction == "edit")
                    {
                        lModel = lBll.GetModelByID(Convert.ToInt32(strNewsID));
                        if (lModel != null)
                        {
                            DcmsOperateModel objDcmsOperateModel = new DcmsOperateModel();
                            objDcmsOperateModel.UpdateModelByControls(lModel, this.Form.Controls);
                            lBll.Update(lModel);
                            Session.Add("Message", "信息已经编辑成功，请确认！");
                            Session.Add("OtherMessage", "发布信息");
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        lModel = new Dcms_LinkModel();
                        DcmsOperateModel.InitModel(lModel);
                        lModel.Link_CateId = cModel.Cate_ID;
                        DcmsOperateModel objDcmsOperateModel = new DcmsOperateModel();
                        objDcmsOperateModel.UpdateModelByControls(lModel, this.Form.Controls);
                        lBll.AddModel(lModel);
                        Session.Add("Message", "信息已经添加成功，请确认！");
                        Session.Add("OtherMessage", "继续添加");
                    }
                    Session.Add("Url", "Modle/Link/ListLink" + cModel.Cate_ModelKeyId + ".aspx?CatID=" + strCatID);
                    Session.Add("OtherUrl", "Modle/Link/EditLink" + cModel.Cate_ModelKeyId + ".aspx?CatID=" + strCatID + "&action=add");
                    Response.Redirect("../../Message.aspx", false);
                }
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_Modle_Link_EditLink.btnSave_Click", ex);
            throw ex;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        try
        {
            Dcms_CateModel cModel = cBll.GetModelByID(Convert.ToInt32(strCatID));
            if (cModel != null)
            {
                if (cModel.Cate_showtype == 1)
                {
                    lBll.DeleteModelByID(Convert.ToInt32(strNewsID));
                    Session.Add("Message", "信息已经删除成功，请确认！");
                    Session.Remove("OtherMessage");
                    Session.Add("Url", "Modle/Link/ListLink" + cModel.Cate_ModelKeyId + ".aspx?CatID=" + strCatID);
                    Response.Redirect("../../Message.aspx", false);
                }
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_Modle_Link_EditLink.btnDel_Click", ex);
            throw ex;
        }
    }

    /// <summary>
    /// 复制
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCopy_Click(object sender, EventArgs e)
    {
        try
        {
            Dcms_CateModel cModel = cBll.GetModelByID(Convert.ToInt32(strCatID));
            if (cModel != null)
            {
                if (cModel.Cate_showtype == 1)
                {
                    Dcms_LinkModel lModel = lBll.GetModelByID(Convert.ToInt32(strNewsID));
                    if (lModel != null)
                    {
                        DcmsOperateModel objDcmsOperateModel = new DcmsOperateModel();
                        objDcmsOperateModel.UpdateModelByControls(lModel, this.Form.Controls);
                        lBll.AddModel(lModel);
                        Session.Add("Message", "信息已经复制成功，请确认！");
                        Session.Remove("OtherMessage");
                        Session.Add("Url", "Modle/Link/ListLink" + cModel.Cate_ModelKeyId + ".aspx?CatID=" + strCatID);
                        Response.Redirect("../../Message.aspx", false);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_Modle_Link_EditLink.btnCopy_Click", ex);
            throw ex;
        }
    }


    #region 属性
    private string strNewsID
    {
        get
        {
            return Request.QueryString["ID"] + "";
        }
    }

    private string strAction
    {
        get
        {
            return Request.QueryString["action"] + "";
        }
    }

    private string strCatID
    {
        get
        {
            return Request.QueryString["CatID"] + "";
        }
    }
    #endregion
}
